package com.invengo.module_check.ui.fragment.opeartion;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.repsitory.business.BusinessRepsitory;
import com.invengo.module_check.ui.fragment.opeartion.item.OpBookInfoItemViewModel;
import com.invengo.module_check.ui.fragment.opeartion.item.OpFirstPosItemViewModel;
import com.invengo.module_check.ui.fragment.opeartion.item.OpShelfInfoItemViewModel;

import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.collections.MergeObservableList;

public class OpeartionInfoViewModel extends BaseViewModel<BusinessRepsitory> {
    public String bookTip = "共 <span style=\"color:#FF7E5A; font-weight:bold;\">%d</span> 本";
    public String shelfTip = "共 <span style=\"color:#5DBB80; font-weight:bold;\">%d</span> 层";

    public ObservableField<String> title = new ObservableField<>("");


    public ObservableList<OpBookInfoItemViewModel> bookItemList = new ObservableArrayList<>();
    public ObservableList<OpShelfInfoItemViewModel> shelfItemList = new ObservableArrayList<>();
    public ObservableList<OpFirstPosItemViewModel> firstPosList = new ObservableArrayList<>();

    public MergeObservableList<Object> itemList = new MergeObservableList<>()
            .insertList(bookItemList)
            .insertList(firstPosList)
            .insertList(shelfItemList);

    //给RecyclerView添加ItemBinding
    public ItemBinding<Object> itemBinding = ItemBinding.of((itemBinding, position, item) -> {
        if (item.getClass().equals(OpBookInfoItemViewModel.class)) {
            itemBinding.set(BR.viewModel, R.layout.item_op_book_info);
        } else if (item.getClass().equals(OpShelfInfoItemViewModel.class)) {
            itemBinding.set(BR.viewModel, R.layout.item_op_down_info);
        } else if (item.getClass().equals(OpFirstPosItemViewModel.class)) {
            itemBinding.set(BR.viewModel, R.layout.item_op_firstpos_info);
        }
    });

    public UIChangeObservable ui = new UIChangeObservable();


    public class UIChangeObservable {
        public SingleLiveEvent<String> tipEvent = new SingleLiveEvent<>();
    }


    public OpeartionInfoViewModel(@NonNull Application application) {
        super(application, BusinessRepsitory.getInstance());
    }

    public int getPosition(ItemViewModel item) {
        return itemList.indexOf(item);
    }

    public void updateBookBeans(List<BookBean> beans) {
        ui.tipEvent.postValue(String.format(bookTip, beans.size()));
        for (BookBean bean : beans) {
            bookItemList.add(new OpBookInfoItemViewModel(this, bean, bean.getOldShelfName()));
        }
    }

    public void updateChange(List<BookBean> beans) {
        ui.tipEvent.postValue(String.format(bookTip, beans.size()));
        for (BookBean bean : beans) {
            bean.setTip("");
            bookItemList.add(new OpBookInfoItemViewModel(this, bean, bean.getOldShelfName()));
        }
    }

    public void updateShelfBeans(Map<String, List<BookBean>> collect) {
        for (String key : collect.keySet()) {
            shelfItemList.add(new OpShelfInfoItemViewModel(this, key, collect.get(key).size()));
        }
    }

    public void updateShelfBeans(List<ShelfBean> beans) {
        ui.tipEvent.postValue(String.format(shelfTip, beans.size()));
        for (ShelfBean bean : beans) {
            firstPosList.add(new OpFirstPosItemViewModel(this, bean));
        }
    }

    public void showTitle(String title) {
        this.title.set(title);
    }
}
